package org.metasketch.demo.diagrams.ATD;

import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.StrokeType;
import javafx.scene.transform.Rotate;

import org.metasketch.demo.model.TransactionKind;
import org.metasketch.diagram.javafx.graphics.BaseGraphic;
import org.metasketch.diagram.javafx.graphics.CustomLabel;
import org.metasketch.diagram.javafx.graphics.Resizableness;
import org.metasketch.editor.toolbox.IPointCalculator;
import org.metasketch.graphics.GraphicNode;
import org.metasketch.graphics.Point;
import org.metasketch.model.CommonIdentified;

public class TransactionKindNode extends BaseGraphic implements
		IPointCalculator {

	CustomLabel nameLabel = new CustomLabel("Untitled");
	CustomLabel identifierLabel = new CustomLabel("Untitled");

	Rectangle rect;// = new Rectangle();

	Color lineColor = Color.BLACK;
	Color backColor = Color.web("f5f5f5");
	// Color darkBackColor = Color.web("808080");
	private Circle circle;

	int side = 30;
	private Rotate rz;

	public TransactionKindNode() {
		super(new GraphicNode(0, 0, 80, 80));

		setResizable(Resizableness.HORIZONTAL);
	}

	@Override
	protected void continueSetup() {

		setupGraphic(graphicNode.getX(), graphicNode.getY(),
				graphicNode.getWidth(), graphicNode.getHeight());

	}

	private void setupGraphic(int x, int y, int width, int height) {

		this.width = width;
		this.height = height;

		setX(x);
		setY(y);

		rect = new Rectangle(1, 1, 80, 80);
		rect.setStroke(lineColor);
		rect.setStrokeWidth(2);
		rect.setStrokeType(StrokeType.CENTERED);
		// rect.setStyle("-fx-stroke-style:dashed;");
		// rect.setStyle("-fx-stroke-dash-array: 2 4;");
		rect.setFill(backColor);

		circle = new Circle();
		circle.setStroke(lineColor);
		circle.setStrokeWidth(2);
		circle.setFill(backColor);

		nameLabel.setGraphic(this);
		nameLabel.setVerticallyCentered(false);
		nameLabel.setFieldNameEditing("name", true);

		identifierLabel.setGraphic(this);
		identifierLabel.setVerticallyCentered(false);
		identifierLabel.setFieldNameEditing("identifier", true);

		rz = new Rotate();
		rz.setAxis(Rotate.Z_AXIS);
		rz.setAngle(45.0);
		rect.getTransforms().add(rz);

		getChildren().addAll(circle, rect, nameLabel, identifierLabel);
	}

	@Override
	protected void resizeGraphic() {

		int fullSide = side * 2;
		int transY = height - fullSide;
		int centerX = width / 2 - side;
		int centerY = height / 2 - side + transY + 2;

		int rectSide = (int) (((double) fullSide) / Math.sqrt(2.0));

		rect.setWidth(rectSide);
		rect.setHeight(rectSide);

		// rect.getTransforms().add(Transform.rotate(45, centerX, centerY));

		// rect.getTransforms().clear();

		rz.setPivotX(rectSide / 2);
		rz.setPivotY(rectSide / 2);

		rect.setTranslateX(centerX + 9);
		rect.setTranslateY(transY + 8);

		circle.setTranslateX(centerX);
		circle.setTranslateY(transY);
		circle.setCenterX(side);
		circle.setCenterY(side);
		circle.setRadius(side);

		identifierLabel.setTranslateY(transY + 18);

		nameLabel.resize();
		identifierLabel.resize();

	}

	@Override
	protected void refreshModelGraphic() {
		TransactionKind role = (TransactionKind) semantic;

		// nameLabel.setText(role.getName());
		// identifierLabel.setText(role.getIdentifier());

		nameLabel.refreshModelGraphic();
		identifierLabel.refreshModelGraphic();

	}

	@Override
	public Point getClosestPoint(int x, int y) {

		Point coords = getCompleteCoordinatesThroughParent();

		double cX = coords.getX() + circle.getTranslateX()
				+ circle.getCenterX();
		double cY = coords.getY() + circle.getTranslateY()
				+ circle.getCenterY();

		double vX = x - cX;
		double vY = y - cY;
		double magV = Math.sqrt(vX * vX + vY * vY);
		double aX = cX + vX / magV * circle.getRadius();
		double aY = cY + vY / magV * circle.getRadius();

		return new Point((int) aX, (int) aY);
	}

	@Override
	public Point getCenter() {

		Point coords = getCompleteCoordinatesThroughParent();

		return new Point(
				(int) (coords.getX() + circle.getTranslateX() + circle
						.getCenterX()),
				(int) (coords.getY() + circle.getTranslateY() + circle
						.getCenterY()));
	}

	
}
